<template>
  <v-layout class="setting-container" justify-start align-start wrap>
    <v-flex md8 pt-4>
      <v-layout justify-start align-center wrap px-5>
        <v-flex md12>
          <h2 class="setting-title">汇率设置</h2>
        </v-flex>
        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">汇率</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.exchange" solo hide-details></v-text-field>
            <div class="ml-2">元</div>
          </v-flex>
          <v-flex md5>
            <span class="annotation">* USD兑换RMB</span>
          </v-flex>
          <v-flex md3>
            
          </v-flex>
          
        </v-flex>

        <v-flex md12>
          <h2 class="setting-title">任务参数设置</h2>
        </v-flex>
        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">每天同IP买手数量限制</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.t_month_buyer_num" solo hide-details></v-text-field>
            <div class="ml-2">条</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*每天同一IP最多买手多少条任务，默认为6条</span>
          </v-flex>
        </v-flex>

        <!-- <v-flex md12 d-flex mb-2 align-center justify-start>
          
          <v-flex md4>
            <span class="subTitle title-color">每月同IP买手同一店铺限制</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.sameIp" solo hide-details></v-text-field>
            <div class="ml-2">条</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*每月同一IP只能买手同一掌柜多少条任务，默认2个</span>
          </v-flex>
        </v-flex>-->

        <!-- <v-flex md12 d-flex mb-2 align-center justify-start>
          
          <v-flex md4>
            <span class="subTitle title-color">同用户买手同一商品需间隔</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.sameIp" solo hide-details></v-text-field>
            <div class="ml-2">天</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*限制同一登录用户账号买手同一商品需要间隔多少天，填0不限</span>
          </v-flex>
        </v-flex>-->

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">同用户买手同一店铺需间隔</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field
              type="text"
              v-model="setting.t_buyer_shop_interval_time"
              solo
              hide-details
            ></v-text-field>
            <div class="ml-2">天</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*限制同一登录用户账号买手同一店铺的任意商品需要间隔多少天，填0不限</span>
          </v-flex>
        </v-flex>

        <v-flex md12>
          <h2 class="setting-title">买号参数设置</h2>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">同买号天最多买手数量</span>
          </v-flex>
          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.b_day_take_num" solo hide-details></v-text-field>
            <div class="ml-2">条</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*同一买号每天最多买手多少条任务，默认为6条</span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">同买号周最多买手数量</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.b_week_take_num" solo hide-details></v-text-field>
            <div class="ml-2">条</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*同一买号每周最多买手多少条任务，默认为30条</span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">同买号月最多买手数量</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.b_no_take_n_day" solo hide-details></v-text-field>
            <div class="ml-2">条</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*同一买号每月最多买手多少条任务，默认为60条</span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">N天内不能买手同一宝贝</span>
          </v-flex>

          <v-flex md1 d-flex align-center>
            <v-text-field type="text" v-model="setting.b_no_take_n_day" solo hide-details></v-text-field>
            <div class="ml-2">天</div>
          </v-flex>
          <v-flex md7>
            <span class="annotation">*限制同一买号几天内不能买手同一产品，默认为30天</span>
          </v-flex>
        </v-flex>

        <v-flex md12>
          <h2 class="setting-title">审核设置</h2>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">店铺是否需要审核</span>
          </v-flex>
          <v-flex md2 d-flex align-center>
            <v-radio-group v-model="setting.shop_info_check" :column="false">
              <v-radio
                :label="item.text"
                :value="item.value"
                color="primary"
                v-for="(item, i) in judgeItems"
                :key="i"
              ></v-radio>
            </v-radio-group>
          </v-flex>
          <v-flex md6>
            <span class="annotation">* 店铺是否需要审核</span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">用户是否需要审核</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-radio-group v-model="setting.user_info_check" :column="false">
              <v-radio
                :label="item.text"
                :value="item.value"
                color="primary"
                v-for="(item, i) in judgeItems"
                :key="i"
              ></v-radio>
            </v-radio-group>
          </v-flex>
          <v-flex md6>
            <span class="annotation">* 用户是否需要审核</span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">资金流水是否需要审核</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-radio-group v-model="setting.profit_income_check" :column="false">
              <v-radio
                :label="item.text"
                :value="item.value"
                color="primary"
                v-for="(item, i) in judgeItems"
                :key="i"
              ></v-radio>
            </v-radio-group>
          </v-flex>
          <v-flex md6>
            <span class="annotation">* 资金流水是否需要审核</span>
          </v-flex>
        </v-flex>

        <v-flex md12>
          <h2 class="setting-title">分成设置</h2>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">佣金分成方式</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-radio-group v-model="setting.divideType" :column="false">
              <v-radio
                :label="item.text"
                :value="item.value"
                color="primary"
                v-for="(item, i) in divisionItems"
                :key="i"
              ></v-radio>
            </v-radio-group>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">平台</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-text-field type="number" v-model="setting.platform_income" solo hide-details></v-text-field>
            <div class="ml-2">%</div>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>
        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">买手</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-text-field type="number" v-model="setting.buyer_income" solo hide-details></v-text-field>
            <div class="ml-2">%</div>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>
        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">直接代理</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-text-field type="text" v-model="setting.direct_representation" solo hide-details></v-text-field>
            <div class="ml-2">%</div>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>
        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">间接代理</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-text-field type="number" v-model="setting.indirect_agency" solo hide-details></v-text-field>
            <div class="ml-2">%</div>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>

        <v-flex md12 d-flex mb-2 align-center justify-start>
          <v-flex md4>
            <span class="subTitle title-color">合计</span>
          </v-flex>

          <v-flex md2 d-flex align-center>
            <v-text-field
              type="number"
              disabled
              v-model="divisionTotal"
              solo
              hide-details
              :class="{'error' :divisionTotal.toString() !== '100'}"
              :color="divisionTotal.toString() == '100' ? 'black' : 'red'"
            ></v-text-field>
            <div class="ml-2">%</div>
          </v-flex>
          <v-flex md6>
            <span class="annotation"></span>
          </v-flex>
        </v-flex>

        <v-flex md12 mt-4 d-flex align-center justify-center>
          <v-flex md4>
            <v-btn large round color="primary" @click="onSave">
              <span class="title">保存</span>
            </v-btn>
          </v-flex>
        </v-flex>
      </v-layout>
    </v-flex>
  </v-layout>
</template>


<script lang="ts">
// 使用类装饰器
import { Component, Vue } from "vue-property-decorator";
import { KV } from "@/utils/type";
import { Todo, Admin, System } from "@/api";
declare const $Vue: Vue;
import { extendRoutes, menuRoutes, fullRoutes } from "@/routes/routes";
import { buildMenu } from "@/utils/commonUtils";
import { SysConfig } from '../../api/bean';

@Component
export default class AddRole extends Vue {

  private judgeItems: Array<KV> = [
    {
      text: "是",
      value: '0'
    },
    {
      text: "否",
      value: '1'
    }
  ];

  private divisionItems: Array<KV> = [
    {
      text: "按比例分成",
      value: 1,
    },
  ];

  private setting: any = {
    exchange: 7.3,
    buyer_income: 25,
    platform_income: 25,
    direct_representation: 25,
    indirect_agency: 25,
    shop_info_check: 1, // 店铺是否需要审核
    profit_income_check: 1, // 资金流水是否需要审核
    user_info_check: 1, // 用户是否需要审核
    b_no_take_n_day: 30, // N天内不能买手同一宝贝
    b_month_take_num: 6, // 同买号月最多买手数量
    b_week_take_num: 6, // 同买号周最多买手数量
    b_day_take_num: 6, // 同买号天最多买手数量
    t_buyer_shop_interval_time: 1, // 同用户买手同一店铺需间隔
    t_month_buyer_num: 1, // 每月同IP买手同一店铺限制
    is_check_common: 1, // 是否审核
    task_order_astrict: 1, // 订单限制
  };

  protected mounted() {
    this.getSysSettinglist();
  }

  private get divisionTotal() {
    return Number(this.setting.buyer_income) + Number(this.setting.direct_representation) + Number(this.setting.indirect_agency) + Number(this.setting.platform_income)
  }

  private validate(): boolean {
    let pass = true;
    return pass;
  }

  private onSave() {
    if (this.validate()) {
    }

    let setting: any = {
      shop_info_check: this.setting.shop_info_check,
      profit_income_check: this.setting.profit_income_check,
      user_info_check: this.setting.user_info_check,
      b_no_take_n_day: this.setting.b_no_take_n_day,
      b_month_take_num: this.setting.b_month_take_num,
      b_week_take_num: this.setting.b_week_take_num,
      b_day_take_num: this.setting.b_day_take_num,
      t_buyer_shop_interval_time: this.setting.t_buyer_shop_interval_time,
      t_month_buyer_num: this.setting.t_month_buyer_num,
      is_check_common: this.setting.is_check_common,
      task_order_astrict: this.setting.task_order_astrict,
    };

    for (let key in setting) {
      
      this.saveSystemSetting(key, setting[key])
    }

    this.updateExchange();
    this.updateCommision();
  }

  private handleSysConfig(list: Array<SysConfig>) {
    let excludes = ['buyer_income', 'platform_income', 'direct_representation', 'indirect_agency'];
    list.forEach((item: SysConfig) => {
      let confName = item.confName;
      console.log(confName)
      if (this.setting[confName]) {
        if (excludes.indexOf(confName) === -1) {
          this.setting[confName] = item.confValue;
        } else {
          this.setting[confName] = Number(item.confValue) * 100;
        }
      }
    })
  }

  private async getSysSettinglist() {
    const params: any = {

    };
    const { data } = await System.getSysSettinglist.r(params);
    console.log("getSysSettinglist", data);
    if (data.result === 0) {
      this.handleSysConfig(data.data)
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }

  private async saveSystemSetting(confName: string, confValue: any) {
    const params: any = {
      confName,
      confValue
    };
    const { data } = await System.saveSystemSetting.r(params);
    console.log("saveSystemSetting", data);
    if (data.result === 0) {
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }

  /**
   * 修改汇率
   */
  private async updateExchange() {
    const params: SysConfig = {
      confName: "exchange",
      confValue: this.setting.exchange,
    };
    const { data } = await System.updateExchange.r(params);
    console.log("updateExchange", data);
    $Vue.$emit("snackbar", {
      message: data.message
    });
    if (data.result === 0) {

    } else {

    }
  }

  /**
   * 修改佣金比例
   */
  private async updateCommision() {
    const params: Array<SysConfig> = [{
      confName: 'platform_income',
      confValue: (this.setting.platform_income / 100).toString(),
    }, {
      confName: 'buyer_income',
      confValue: (this.setting.buyer_income / 100).toString(),
    }, {
      confName: 'direct_representation',
      confValue: (this.setting.direct_representation / 100).toString(),
    }, {
      confName: 'indirect_agency',
      confValue: (this.setting.indirect_agency / 100).toString(),
    },];
    const { data } = await System.updateCommision.r(params);
    console.log("updateCommision", data);
    if (data.result === 0) {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    } else {
      $Vue.$emit("snackbar", {
        message: data.message
      });
    }
  }


}
</script>

<style lang="less" scoped>
.setting-container {
  .setting-title {
    color: #676767;
    font-size: 1rem;
    text-align: left;
  }
  .annotation {
    color: #9e9e9e;
    font-size: 0.8rem;
  }
}
</style>


